<?php

class UserLogin {

    private $daten;
    private $loggin_error;
    private $login = false;

    public function __construct($match) {
        $this->daten = $match;
    }

    public function getWort(RainTPL $design) {
        if(isset($this->daten['City_id'])){
            $city = new Db_City();
            $selectetcity = $city->getCity($this->daten['City_id']);
             
            $_SESSION ['Userlat']    = $selectetcity->lat;
            $_SESSION ['Userlng']    = $selectetcity->lng;
            $_SESSION ['UserCity']   = $selectetcity->name;
            $_SESSION ['UserCityId'] = $selectetcity->id;
            
            if(isset($_COOKIE['GFW']['uci'])){
                $_COOKIE['GFW']['uci'] = $selectetcity->id;
            }else{
                setcookie("GFW[uci]",$selectetcity->id);
            }
            
            HTTP::redirect(ConfigFile::Get('BASE_DIR'));
        }
         
        if ($design->post->gfw_email && $design->post->gfw_password) {
            $loginObj = new Db_User();
            $this->login = $loginObj->LoginCheck ( $design->post->gfw_email, $design->post->gfw_password);

            if ($this->login) {
                $_SESSION ['UserId'] = $this->login->id;
                $_SESSION ['UserName'] = $this->login->username;
                $_SESSION['session_check'] = NULL;
                 
                HTTP::redirect(ConfigFile::Get('BASE_DIR'));
            } else {
                $this->loggin_error = LOGIN_ERROR;
            }
        }elseif ($design->get->out){
            unset($_SESSION ['UserId']);
            unset($_SESSION ['UserName']);

            HTTP::redirect(ConfigFile::Get('BASE_DIR'));
        }

        if ($this->loggin_error) {
            $design->assign ( 'Error', $this->loggin_error );
        }

        $design->assign ('css',"loginscreen");
        $design->assign ('js',"jsLoginScreen");

        if( $cache = $design->cache( $design->ln.'/login/login', $expire_time = 0) )
        {
            echo $cache;
        }
        else
        {
            $design->draw( $design->ln.'/login/login' );
        }
    }

}
?>